<template>
    <div class="waybill-search-container">
        <el-form :model="ruleForm" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <div class="operation-buttons">
                <el-popconfirm confirm-button-text='好的' cancel-button-text='不用了' icon="el-icon-info" icon-color="red"
                    title="这是一段内容确定删除吗？">
                    <el-button slot="reference">批量操作</el-button>
                </el-popconfirm>
                <el-popconfirm confirm-button-text='好的' cancel-button-text='不用了' icon="el-icon-info" icon-color="red"
                    title="这是一段内容确定导入吗？">
                    <el-button slot="reference"> 导入 </el-button>
                </el-popconfirm>
                <el-button type="primary" @click="exportExcel()"> 导出 </el-button>

                <el-popconfirm confirm-button-text='好的' cancel-button-text='不用了' icon="el-icon-info" icon-color="red"
                    title="这是一段内容确定打印吗？">
                    <el-button slot="reference"> 打印 </el-button>
                </el-popconfirm>
            </div>
            <el-form-item label="订单状态">
                <el-select v-model="ruleForm.orderStatus" placeholder="全部">
                    <el-option label="已签收" value="已签收"></el-option>
                    <el-option label="待审核" value="待审核"></el-option>
                    <el-option label="待调度" value="待调度"></el-option>
                    <el-option label="运输中" value="运输中"></el-option>
                    <el-option label="已拒绝" value="已拒绝"></el-option>
                    <el-option label="已取消" value="已取消"></el-option>

                </el-select>
            </el-form-item>
            <el-form-item label="始发地" prop="origin">
                <el-cascader v-model="originSelected" :options="pcaTextArr" @change="handleOriginChange">
                </el-cascader>
            </el-form-item>
            <el-form-item label="目的地" prop="destination">
                <el-cascader v-model="destinationSelected" :options="pcaTextArr" @change="handleDestinationChange">
                </el-cascader>
            </el-form-item>
            <el-form-item label="签收日期">
                <el-col :span="11">
                    <el-form-item prop="receiptTime1">
                        <el-date-picker type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期"
                            v-model="ruleForm.receiptTime1" style="width: 100%;"></el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col class="line" :span="2">-</el-col>
                <el-col :span="11">
                    <el-form-item prop="receiptTime2">
                        <el-date-picker type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期"
                            v-model="ruleForm.receiptTime2" style="width: 100%;"></el-date-picker>
                    </el-form-item>
                </el-col>
            </el-form-item>
            <el-form-item>
                <el-input placeholder="请输入关键词搜索" v-model="ruleForm.orderId"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="getAll('ruleForm')">搜索</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
        <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%"
            @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column prop="orderId" label="订单号">
            </el-table-column>
            <el-table-column prop="customer" label="客户">
            </el-table-column>
            <el-table-column prop="origin" label="始发地">
            </el-table-column>
            <el-table-column prop="destination" label="目的地">
            </el-table-column>
            <el-table-column prop="orderStatus" label="订单状态">
            </el-table-column>
            <el-table-column prop="shippingAddress" label="发货地址">
            </el-table-column>
            <el-table-column prop="receivingAddress" label="收货地址">
            </el-table-column>
            <el-table-column prop="totalQuantity" label="订单数量">
            </el-table-column>
            <el-table-column prop="totalQuantity" label="签收数量">
            </el-table-column>
            <el-table-column prop="signerName" label="签收人">
            </el-table-column>
            <el-table-column prop="receiptTime" label="签收时间">
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="100">
                <template slot-scope="scope">
                    <el-button @click="toXiangQing(scope.row)" type="text" size="small">详情</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageNum"
            :page-sizes="[10, 20, 30, 40]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
        <el-dialog :title="form2.id == null ? '详情' : '详情'" :visible.sync="dialogTableVisible" width="100%">
            <el-form :model="form2">
                <el-descriptions title="基础信息">
                    <el-descriptions-item label="订单状态">{{ form2.orderStatus }}</el-descriptions-item>
                    <el-descriptions-item label="客户">{{ form2.customer }}</el-descriptions-item>
                    <el-descriptions-item label="运单号">{{ form2.waybillId }}</el-descriptions-item>
                    <el-descriptions-item label="订单号">{{ form2.orderId }}</el-descriptions-item>
                    <el-descriptions-item label="始发地">{{ form2.origin }}</el-descriptions-item>
                    <el-descriptions-item label="目的地">{{ form2.destination }}</el-descriptions-item>
                    <el-descriptions-item label="配送方式">{{ form2.deliveryMethod }}</el-descriptions-item>
                    <el-descriptions-item label="发车时间">{{ form2.actualDepartureTime }}</el-descriptions-item>
                    <el-descriptions-item label="收货地址">{{ form2.receivingAddress }}</el-descriptions-item>
                    <el-descriptions-item label="到站时间">{{ form2.actualArrivalTime }}</el-descriptions-item>
                    <el-descriptions-item label="发" label-class-name="sender-separator"
                        content-class-name="sender-separator-content"></el-descriptions-item>
                    <el-descriptions-item label="联系人姓名">{{ form2.senderContactName }}</el-descriptions-item>
                    <el-descriptions-item label="联系电话">{{ form2.senderContactPhone }}</el-descriptions-item>
                    <el-descriptions-item label="发货地址">{{ form2.shippingAddress }}</el-descriptions-item>
                    <el-descriptions-item label="计划发货时间">{{ form2.plannedDepartureTime }}</el-descriptions-item>
                    <el-descriptions-item label="收" label-class-name="receiver-separator"
                        content-class-name="receiver-separator-content"></el-descriptions-item>
                    <el-descriptions-item label="联系人姓名">{{ form2.receiverContactName }}</el-descriptions-item>
                    <el-descriptions-item label="联系电话">{{ form2.receiverContactPhone }}</el-descriptions-item>
                    <el-descriptions-item label="收货地址">{{ form2.receivingAddress }}</el-descriptions-item>
                    <el-descriptions-item label="计划收货时间">{{ form2.plannedArrivalTime }}</el-descriptions-item>
                </el-descriptions>
                <el-descriptions title="货物明细" direction="vertical" :column="6"  border>
                    <el-descriptions-item label="序号">{{ form2.id }}</el-descriptions-item>
                    <el-descriptions-item label="货物名称">{{ form2.cargoName }}</el-descriptions-item>
                    <el-descriptions-item label="数量" :span="2">{{ form2.quantity }}</el-descriptions-item>
                    <el-descriptions-item label="体积（m3）">
                        {{ form2.volume }}
                    </el-descriptions-item>
                    <el-descriptions-item label="重量（kg）">{{ form2.weight }}</el-descriptions-item>
                </el-descriptions>
                <el-descriptions title="签收信息">
                    <el-descriptions-item label="签收数量">{{ form2.totalQuantity }}</el-descriptions-item>
                    <el-descriptions-item label="签收类型">{{ form2.receiptType }}</el-descriptions-item>
                    <el-descriptions-item label="签收人">{{ form2.signerName }}</el-descriptions-item>
                    <el-descriptions-item label="签收人手机号">
                        {{ form2.signerPhone }}
                    </el-descriptions-item>
                    <el-descriptions-item label="签收人身份证">{{ form2.signerIdNumber }}</el-descriptions-item>
                    <el-descriptions-item label="签收时间">{{ form2.receiptTime }}</el-descriptions-item>
                    <el-descriptions-item label="签收备注">{{ form2.receiptRemark }}</el-descriptions-item>
                    <el-descriptions-item label="签收单据">{{ form2.receiptDocument }}</el-descriptions-item>
                </el-descriptions>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogTableVisible = false"> 返回 </el-button>
                <el-button type="primary" @click="toEdit()"> 编辑 </el-button>
                <el-button type="primary" @click="toAddOrUpdate()"> 导出 </el-button>
                <el-button type="primary" @click="toAddOrUpdate()"> 打印 </el-button>
                <el-button @click="toRetern()" type="primary">取消调度</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import axios from 'axios';
import { pcaTextArr } from 'element-china-area-data';
export default {
    data() {
        return {
            ruleForm: {},
            tableData: [],
            multipleSelection: [],
            originSelected: [],
            destinationSelected: [],
            pcaTextArr: pcaTextArr,
            options: [],
            pageNum: 1,
            pageSize: 10,
            total: 0,
            dialogTableVisible: false,
            form2: {},
            Waybill: [],
            PickupOrder: [],
        };
    },
    methods: {
        handleOriginChange(val) {
            // 将级联选择的数组值转换为字符串（如：["省", "市", "区"] -> "省/市/区"）
            this.originSelected=val;
            this.ruleForm.origin = val ? val.join('/') : '';
            // this.ruleForm.origin = val ? this.convertCodesToText(val) : '';
        },
        handleDestinationChange(val) {
            this.destinationSelected=val;
            this.ruleForm.destination = val ? val.join('/') : '';
            // this.ruleForm.destination = val ? this.convertCodesToText(val) : '';
        },
        exportExcel(){
            const url = '/api/om/receipt/exportExcel';
            window.location.href = url;
        },
        toXiangQing(row) {
            axios.post("/api/om/receipt/XiangQing", this.form2,).then((res) => {
                this.form2 = JSON.parse(JSON.stringify(row));
                this.dialogTableVisible = true;
            });
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        resetForm() {
            this.ruleForm = {};
            this.getAll();
        },
        getAll() {
            axios.post(`/api/om/receipt/getAll?pageNum=${this.pageNum}&pageSize=${this.pageSize}`, this.ruleForm).then((res) => {
                this.tableData = res.data.records;
                this.total = res.data.total;
            });
        },
        handleSizeChange(val) {
            console.log(`每页 val 条`);
            this.pageSize = val;
            this.getAll();
        },
        handleCurrentChange(val) {
            console.log(`每页 val 条`);
            this.pageNum = val;
            this.getAll();
        },
        findWaybill() {
            axios.post('/api/om/waybill/findWaybill').then((res) => {
                this.Waybill = res.data;
            }).catch((err) => {
                console.log(err);
                this.$message.error('请求出错: ' + err.message);
            });
        },
        findPickupOrder() {
            axios.post('/api/om/pickup-order/findPickupOrder').then((res) => {
                this.PickupOrder = res.data;
            }).catch((err) => {
                console.log(err);
                this.$message.error('请求出错: ' + err.message);
            });
        },
    },
    created() {
        this.getAll();
        this.findWaybill();
        this.findPickupOrder();
    },
}
</script>
<style>
.sender-separator {
    background: #ff9138;
    padding: 10px;
    text-align: center;
    color: #ffffff;
}

.sender-separator-content {
    background: #fff4eb;
}

.receiver-separator {
    background: #2ed477;
    padding: 10px;
    text-align: center;
    color: #ffffff;
}

.receiver-separator-content {
    background: #ebfbf2;
}

.waybill-search-container {
    padding: 20px;
    background-color: #f9f9f9;
    border: 1px solid #e4e7ed;
    border-radius: 4px;
}

.waybill-search-container {
    padding: 20px;
    background-color: #f9f9f9;
    border: 1px solid #e4e7ed;
    border-radius: 4px;
}

.operation-buttons {
    margin-bottom: 10px;
}

.line {
    text-align: center;
}
</style>